from excel_util import ExcelUtil


def read_line(data_array, line):
    data = line[2:][:-2].split('],[')
    for item in data:
        item_arry = item[1:][:-1].split('","')
        data_array.append(item_arry)
    return data_array


def read_data(title_file, data_file):
    with open(title_file, 'r', encoding='utf8') as dict_infos:
        dict_array = dict_infos.readlines()
        dict_list = [item.strip('\n')
                     for item in dict_array if not item.startswith('#')]
        print(dict_list)

    line_num = 0
    data_array = []
    with open(data_file, 'r', encoding='utf8') as data_result:
        data_line = data_result.readline()
        data_line = data_line[:-1]
        line_num += 1
        print('Line num:', line_num)
        read_line(data_array, data_line)
        while (data_line):
            line_num += 1
            data_line = data_result.readline()[:-1]
            read_line(data_array, data_line)
            print('Line num:', line_num)

    return (dict_list, data_array)


def writeExcel(out_file, headers, data):
    excelUtil = ExcelUtil(out_file)
    excelUtil.write_data('sheet1', header=headers, data=data)
    excelUtil.close()

    print('OK')


if __name__ == '__main__':

    types = [{
        'xmlname': 'TRWRFZZXXMJZXX',
        'pageNum': 961,
        'fileName': '土壤污染_data.txt',
        'titleName': '土壤污染.txt',
        'outFile': '土壤污染防治专项项目进展信息.xlsx'
    }, {
        'xmlname': 1611227966117,
        'pageNum': 2799,
        'fileName': '大气污染_data.txt',
        'titleName': '大气污染.txt',
        'outFile': '大气污染防治专项项目信息.xlsx'
    }, {
        'xmlname': 'NCHJZZZXXMXX',
        'pageNum': 961,
        'fileName': '农村污染_data.txt',
        'titleName': '农村污染.txt',
        'outFile': '农村环境整治专项项目信息.xlsx'
    }, {
        'xmlname': 1598591571992,
        'pageNum': 2248,
        'fileName': '水污染_data.txt',
        'titleName': '水污染.txt',
        'outFile': '水染防治专项项目信息.xlsx'
    }, {
        'xmlname': 'TRWRFZZXXMXX',
        'pageNum': 787,
        'fileName': '土壤污染防治专项项目信息_data.txt',
        'titleName': '土壤污染防治专项项目信息.txt',
        'outFile': '土壤污染防治专项项目信息2.xlsx'
    }]

    for type_item in types:
        print(type_item)

        title_file = './' + type_item['titleName']
        data_file = './' + type_item['fileName']
        out_file = './result/' + type_item['outFile']

        (dict_list, data_array) = read_data(
            title_file=title_file, data_file=data_file)
        writeExcel(out_file=out_file, headers=dict_list, data=data_array)
